package com.itheima.dubbo.api;

import com.itheima.domain.mongo.FollowUser;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;

/**
 * @author 阿鹏
 * @date 2021/1/31 13:58
 */
@Service
public class FollowUserApiImpl implements FollowUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void followUser(long userId, long followUserId) {
        FollowUser followUser = new FollowUser();
        followUser.setId(ObjectId.get());
        followUser.setUserId(userId);
        followUser.setFollowUserId(followUserId);
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);
    }

    @Override
    public void disFollowUser(long userId, long followUserId) {
        Query query = Query.query(
                Criteria.where("userId").is(userId).
                        and("followUserId").is(followUserId));
        mongoTemplate.remove(query, FollowUser.class);
    }
}
